import {
  Component,
  OnInit,
  Inject,
  Input,
  Output,
  EventEmitter
} from "@angular/core";
import {
  FormBuilder,
  FormControl,
  FormGroup,
  Validators
} from "@angular/forms";
import * as _ from "lodash";
@Component({
  selector: "app-select-good-type",
  templateUrl: "./select-good-type.component.html",
  styleUrls: ["./select-good-type.component.scss"]
})
export class SelectGoodTypeComponent implements OnInit {
  constructor(
    private fb: FormBuilder,
    @Inject("StoreService") private storeService,
    @Inject("NzMessageService") private nzMessageService,
    @Inject("FileService") private fileService
  ) {}
  onContry: any;

  //条件级别
  @Input()
  type = "g2"; // g1 | g2 | g3

  @Input()
  onType: any[] = null;

  @Output()
  onChange: EventEmitter<any> = new EventEmitter();

  options = [];

  contry: any[] = [
    {
      value: "china",
      label: "china",
      children: []
    }
  ];

  hash = {};

  goodType: any[] = [];

  get g1() {
    const ret = this.eachTree(this.goodType, item => {
      const r = {
        label: item.name,
        value: {
          label: item.name,
          value: item.code
        },
        isLeaf: true
      };
      // if (item.children && item.children.length) {
      //   r['children'] = item.children;
      // }
      return r;
    });
    return ret;
  }

  get g2() {
    const ret = this.eachTree(this.goodType, item => {
      const r = {
        label: item.name,
        value: {
          label: item.name,
          value: item.code
        }
      };
      if (this.isG2(item.code)) {
        item.children = null;
        r["isLeaf"] = true;
      }
      if (item.children && item.children.length) {
        r["children"] = item.children;
      }

      return r;
    });
    return ret;
  }

  get g3() {
    const ret = this.eachTree(this.goodType, item => {
      const r = {
        label: item.name,
        value: {
          label: item.name,
          value: item.code
        }
      };
      if (this.isG2(item.code)) {
        if (!item.children || !item.children.length) {
          r["isLeaf"] = true;
        }
      }
      if (this.isG3(item.code)) {
        item.children = null;
        r["isLeaf"] = true;
      }
      if (item.children && item.children.length) {
        r["children"] = item.children;
      }

      return r;
    });
    return ret;
  }

  ngOnInit() {
    this.addListen();
  }

  addListen() {
    this.storeService.subscribe(state => {
      const { user, common } = state;
      if (common.config.goodType) {
        this.goodType = this.initData(
          this.storeService.state.common.config.goodType
        );
        this.initSelect();
      }
    });
  }

  initData(data) {
    return this.eachTree(data, item => {
      const r = {
        ...item
      };
      if (!r.children || !r.children.length) {
        delete r.children;
      }
      return r;
    });
  }

  initSelect() {
    this.options = this[this.type];
  }

  eachTree(arr: any, fn: Function) {
    return arr.reduce((arr, item, i) => {
      arr[i] = fn(item);

      if (arr[i].children && arr[i].children.length) {
        arr[i].children = this.eachTree(arr[i].children, fn);
      }

      return arr;
    }, []);
  }

  onChangeType(e) {
    this.onChange.emit(_.merge([], this.onType));
  }

  isG1(code: string) {
    return code && code.length == 2;
  }
  isG2(code: string) {
    return code && code.length == 4;
  }
  isG3(code: string) {
    return code && code.length == 6;
  }
}
